package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;

import utils.Constants;
import utils.ImageCreator;
import actions.SysTrayActionManager;
import application.Application;



public class GUIWindow extends JFrame implements Constants {
	
	private static final long serialVersionUID = 1275977429950445487L;
	
	private Application theApp = null;
	
	private int width = WINDOW_WIDTH;
	private int height = WINDOW_HEIGHT;
	
	private Toolkit kit;
	
	private JPanel jContentPane = null;
	
	private SysTrayActionManager actionManager = null;
	

	private JLabel logoLabel = null;
	
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu serverMenu = null;
	private JMenu helpMenu = null;
	
	
	public GUIWindow(Application theApp) {
		super();
		
		this.theApp = theApp;
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) {
			setDefaultLookAndFeelDecorated(false);
		}

		actionManager = new SysTrayActionManager(this);
		
		initialize();
		
		try {
			sysTrayCreator();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		actionManager.setStoppedRights();
	}
	
	
	public Application getApplication() {
		return this.theApp;
	}
	
	
	public SysTrayActionManager getActionManager() {
		return this.actionManager;
	}
	
	
	private void initialize() {
		this.setTitle("RLM Server 2.0");
		this.setSize(width, height);
		
		kit = this.getToolkit();
		Dimension dim = kit.getScreenSize();
		this.setLocation( (dim.width-width)/2, (dim.height-height)/2 );
		
		this.setSize(width, height);
		this.setResizable(false);

		URL url = GUIWindow.class.getResource("/icons/update_icon.png");
		this.setIconImage(kit.createImage(url));
		
		this.setJMenuBar(getJJMenuBar());
		
		this.setContentPane(getJContentPane());

	}

	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(10);
			flowLayout.setVgap(10);
			
			jContentPane.setLayout(flowLayout);
		}
		
		jContentPane.add(getLogoLabel());
		
		return jContentPane;
	}
	


	private JLabel getLogoLabel() {
		if (logoLabel == null) {
			ImageIcon logoIcon = ImageCreator.createImageIcon(GUIWindow.class, "/server_background.png");
			logoLabel = new JLabel(logoIcon);
		}
		return logoLabel;
	}
	

	
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getServerMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}
	
	
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.setMnemonic('f');

			fileMenu.add(actionManager.getStatusAction());
			fileMenu.addSeparator();
			fileMenu.add(actionManager.getExitAction());
		}
		return fileMenu;
	}
	
	
	private JMenu getServerMenu() {
		if (serverMenu == null) {
			serverMenu = new JMenu();
			serverMenu.setText("Server");
			serverMenu.setMnemonic('s');

			serverMenu.add(actionManager.getStartAction());
			serverMenu.add(actionManager.getStopAction());
			serverMenu.addSeparator();
			serverMenu.add(actionManager.getSettingsAction());
		}
		return serverMenu;
	}
	
	
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.setMnemonic('h');

			helpMenu.add(actionManager.getAboutAction());
		}
		return helpMenu;
	}
	
	
	
	/* System Tray Section */
	
	private void sysTrayCreator() throws Exception {

		if ( !SystemTray.isSupported() ) {
			System.out.println("YES");
		}
		
		SystemTray tray = SystemTray.getSystemTray();

		URL url = GUIWindow.class.getResource("/icons/update_icon.png");
		Image img = kit.createImage(url);
		
		PopupMenu popUp = new PopupMenu();

		popUp.add(actionManager.getStartAction().getItem());
		popUp.add(actionManager.getStopAction().getItem());
		popUp.addSeparator();
		popUp.add(actionManager.getSettingsAction().getItem());
		popUp.addSeparator();
		popUp.add(actionManager.getExitAction().getItem());
			
		TrayIcon tIcon = new TrayIcon(img, "RLM Update Server", popUp);
		tIcon.setImageAutoSize(true);
		
		tIcon.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					GUIWindow.this.setVisible(true);
				}
			}
		});
		
		if (tray != null) {
			tray.add(tIcon);
		}
	}
	
}
